export const calculateLayout = (containerWidth, items, minColumns = 6) => {
  const maxColumnWidth = 240
  const minColumnWidth = 160
  const minGap = 8

  let columnCount = Math.floor(containerWidth / maxColumnWidth)
  columnCount = Math.max(minColumns, columnCount)
  
  let columnWidth = (containerWidth - (columnCount - 1) * minGap) / columnCount
  columnWidth = Math.min(maxColumnWidth, Math.max(minColumnWidth, columnWidth))
  
  const gap = (containerWidth - columnWidth * columnCount) / (columnCount - 1)
  
  const columns = Array.from({ length: columnCount }, () => [])
  
  items.forEach((item, index) => {
    const columnIndex = index % columnCount
    columns[columnIndex].push({
      ...item,
      height: Math.floor((item.height || 200) * (columnWidth / (item.width || 200)))
    })
  })

  return { columns, columnWidth, gap }
}